<template>
  <el-dialog  :width="'30%'"  v-model="visible" :title="导入其他项目线路" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
             label-width="120px">
      <el-input v-model="dataForm.projectId"  style="display:none;"></el-input>
      <el-form-item label="选择项目" prop="copyProjectId">
        <el-select v-model="dataForm.copyProjectId" filterable placeholder="请选择线路"  clearable @change="projectChange">
          <el-option  v-for="data in dataForm.projectOp"   :key="data.id" :label="data.name" :value="data.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择路线"  prop="lineId">
        <el-select v-model="dataForm.lineId" filterable placeholder="请选择线路"  clearable >
          <el-option  v-for="data in dataForm.lineOp"   :key="data.id" :label="data.name" :value="data.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from "vue";
import baseService from "@/service/baseService";
import {ElMessage} from "element-plus";
import {useRouter} from "vue-router";

const router = useRouter();
const emit = defineEmits(["refreshDataList"]);
const visible = ref(false);
const dataFormRef = ref();
const dataForm = reactive({
    projectId: '',
    copyProjectId:"",
    lineId:"",
    projectOp:[{"id":"","name":""}],
    lineOp:[{"id":"","name":""}]
  }
);
const rules = ref({
  copyProjectId: [
    {required: true, message: '请选择项目', trigger: 'blur'}
  ],
  lineId: [
    {required: true, message: '请选择路线', trigger: 'blur'}
  ]
});

const init = (row?: {}) => {
  debugger
  if (Object.keys(row).length<=0) {
    ElMessage.error({
      message: '请重新选择项目', duration: 500,
      onClose: () => {
        router.replace("/sysproject/project");
      }
    });
    return
  }
  // 获取信息
  baseService.get("sysproject/line/findProSelect").then((res) => {
    Object.assign(dataForm.projectOp, res.data);
  });
  visible.value = true;
  dataForm.projectId = row.projectId
};

const projectChange =()=>{
  // 获取信息
  baseService.get("sysproject/line/findLineSelect",{"proId":dataForm.copyProjectId}).then((res) => {
    Object.assign(dataForm.lineOp, res.data);
  });
}


// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    baseService.get("sysproject/line/copyLine", {"lineId":dataForm.lineId,"thisProjectId":dataForm.projectId}).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
